package com.cloudkinto.service.finance.impl;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.BeanConvert;
import com.cloudkinto.common.DicBusinessConstant;
import com.cloudkinto.common.utils.ExcelUtils;
import com.cloudkinto.common.utils.StringsUtil;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.extentity.OrderBackDto;
import com.cloudkinto.extentity.OrderBackProductDto;
import com.cloudkinto.service.dic.vo.LanguageValueEntity;
import com.cloudkinto.service.file.FileService;
import com.cloudkinto.service.finance.CompanyAccountService;
import com.cloudkinto.service.finance.CostRecordService;
import com.cloudkinto.service.finance.vo.CostRecordListRes;
import com.cloudkinto.service.finance.vo.RepairBillExportVo;
import com.cloudkinto.service.finance.vo.exportVo.*;
import com.cloudkinto.service.orderrepair.OrderRepairService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author ding
 * @since 2020-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class CostRecordServiceImpl extends ServiceImpl<CostRecordDao, CostRecordDo> implements CostRecordService {
    @Autowired
    private CostRecordDao dao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private OrderOutputDao orderOutputDao;
    @Autowired
    private OrderOutputPackageDao orderOutputPackageDao;
    @Autowired
    private OrderOutputProductDao orderOutputProductDao;
    @Autowired
    private StorageDao storageDao;
    @Autowired
    private ProductInfoDao productInfoDao;
    @Autowired
    private OrderInputDao orderInputDao;
    @Autowired
    private StockHistoryDao stockHistoryDao;
    @Autowired
    private OrderWorkDao orderWorkDao;
    @Autowired
    private OrderWorkPriceDao orderWorkPriceDao;
    @Autowired
    private DicBusinessItemDao dicBusinessItemDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private CompanyAccountService companyAccountService;
    @Autowired
    private FbaOrderOutputDao fbaOrderOutputDao;
    @Autowired
    private FbaStorageDao fbaStorageDao;
    @Autowired
    private FbaOrderOutputPackageDao fbaOrderOutputPackageDao;
    @Autowired
    private OrderBackDao orderBackDao;
    @Autowired
    private OrderBackProductDao orderBackProductDao;
    @Autowired
    private ReplacementDao replacementDao;
    @Autowired
    private FbaOrderOutputProductDao fbaOrderOutputProductDao;
    @Autowired
    private ManageBillDao manageBillDao;
    @Autowired
    private HeaderContainerDao headerContainerDao;
    @Autowired
    private ManageHeaderPriceItemDao manageHeaderPriceItemDao;
    @Autowired
    private StorageServiceProjectDao storageServiceProjectDao;
    @Autowired
    private FileService fileService;
    @Autowired
    private OrderRepairService repairService;

    @Override
    public SingleResult addInit() {
        return SingleResult.success("");
    }

    @Override
    public Object add(Object req, Long userId) {
        CostRecordDo entityDo = new CostRecordDo();
        BeanUtils.copyProperties(req, entityDo);
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);
        return handlerAddAfter(entityDo);
    }

    @Override
    public boolean add(int type, String content, double cost, Long shopId, Long companyId, Long thirdId) {
        return this.add(type, content, cost, shopId, companyId, thirdId, 1);
    }

    @Override
    public boolean add(int type, String content, double cost, Long shopId, Long companyId, Long thirdId, Integer status) {
        CostRecordDo costRecordDo = new CostRecordDo();
        costRecordDo.setType(type);
        costRecordDo.setContent(content);
        costRecordDo.setCostAmount(new BigDecimal(cost).setScale(0, RoundingMode.HALF_UP).doubleValue());
        costRecordDo.setShopId(shopId);
        costRecordDo.setCompanyId(companyId + "");
        costRecordDo.setThirdId(thirdId + "");
        costRecordDo.setCreateTime(new Date());
        costRecordDo.setUpdateTime(new Date());
        if (status == 1) {
            costRecordDo.setPayTime(new Date());
        }
        costRecordDo.setStatus(status);
        if (status == 1) {
            handlerAddAfter(costRecordDo);
        }
        int rows = dao.insert(costRecordDo);
        return rows > 0;
    }

    public boolean update(Long id, Integer status, double cost) {
        CostRecordDo costRecordDo = dao.selectById(id);
        //更新
        costRecordDo.setCostAmount(new BigDecimal(cost).setScale(0, RoundingMode.HALF_UP).doubleValue());
        costRecordDo.setStatus(status);
        if (status == 1) {
            costRecordDo.setUpdateTime(new Date());
            costRecordDo.setPayTime(new Date());
            handlerAddAfter(costRecordDo);
        }
        if (status == 1) {
            int rows = dao.updateById(costRecordDo);
            return rows > 0;
        } else {
            return false;
        }
    }


    /**
     * 系统自动余额补齐方法
     * 添加支出记录
     *
     * @param type
     * @param content
     * @param cost
     * @param afterAmount
     * @param shopId
     * @param companyId
     * @return
     */
    @Override
    public boolean addExchange(int type, String content, double cost, double afterAmount, long shopId, Long companyId) {
        CostRecordDo costRecordDo = new CostRecordDo();
        costRecordDo.setAccountType(type);//人民币扣款
        costRecordDo.setType(3);//定制工单
        costRecordDo.setContent(content);
        costRecordDo.setCostAmount(cost);
        costRecordDo.setAfterAmount(afterAmount);
        costRecordDo.setShopId(shopId);
        costRecordDo.setCompanyId(companyId + "");
        costRecordDo.setThirdId(0L + "");
        costRecordDo.setCreateTime(new Date());
        costRecordDo.setStatus(1);
        int rows = baseMapper.insert(costRecordDo);
        return rows > 0;
    }

    //新增校验
    private void addCheck(Object req) {

    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(CostRecordDo entityDo, Long userId) {
//        entityDo.setCreateBy(userId);
        entityDo.setCreateTime(new Date());
//        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
    }

    //将新增的do转换成要返回的实体
    private Object handlerAddAfter(CostRecordDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        if (entityDo.getStatus() == 1) {
            double afterAmount = companyAccountService.costAmount(entityDo.getCompanyId(), entityDo.getCostAmount(), entityDo.getAccountType());
            entityDo.setAfterAmount(afterAmount);
//            else {
//                entityDo.setPayTime(null);
//                entityDo.setStatus(0);
//            }
//            updateById(entityDo);
        }
        return res;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success("");
    }

    @Override
    public Object update(Object req, Long userId) {
        /*CostRecordDo entityDo = dao.selectById(req.getId());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);*/
        return null;
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(CostRecordDo entityDo, Long userId) {
//        entityDo.setUpdateBy(userId);
//        entityDo.setUpdateTime(new Date());
    }

    //做修改之后要执行的业务定制处理
    private Object handlerUpdateAfter(CostRecordDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        CostRecordDo entityDo = dao.selectById(id);
        handlerDeleteBefore(entityDo, userId);
        return dao.deleteById(id);
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(CostRecordDo entityDo, Long userId) {
//        entityDo.setDeleteBy(userId);
//        entityDo.setDeleteTime(new Date());
    }

    @Override
    public Object detail(Long id) {
        CostRecordDo entityDo = dao.selectById(id);
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private Object handlerDetailAfter(Object res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success(this.getFeeType());
    }

    @Override
    public List<LanguageValueEntity> getFeeType() {
        List<LanguageValueEntity> typeList = new ArrayList<>();
        typeList.add(new LanguageValueEntity(0, "出库费"));
        typeList.add(new LanguageValueEntity(1, "入库费"));
        typeList.add(new LanguageValueEntity(2, "仓储费"));
        typeList.add(new LanguageValueEntity(3, "定制费"));
        typeList.add(new LanguageValueEntity(4, "退货费"));
        typeList.add(new LanguageValueEntity(5, "FBA转运"));
        typeList.add(new LanguageValueEntity(6, "头程运费"));
        typeList.add(new LanguageValueEntity(7, "维修订单"));
        return typeList;
    }

    @Override
    public List<CostRecordListRes> getList(Map<String, String> map) {
        QueryWrapper<CostRecordDo> wrapper = queryBuild(map);
        List<CostRecordDo> list = this.list(wrapper);
        List<CostRecordListRes> result = handlerListAfter(list);
        return result;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<CostRecordDo> wrapper = queryBuild(map);
        IPage<CostRecordDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<CostRecordListRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    private Map<Integer, String> getTypeMap() {
        Map<Integer, String> typeMap = new HashMap<>();
        typeMap.put(0, "出库费");
        typeMap.put(1, "入库费");
        typeMap.put(2, "仓储费");
        typeMap.put(3, "定制费");
        typeMap.put(4, "退货费");
        typeMap.put(5, "FBA转运");
        typeMap.put(6, "头程运费");
        typeMap.put(7, "维修订单");
        return typeMap;
    }

    private Map<Long, String> getTracIdMap() {
        LambdaQueryWrapper<DicBusinessItemDo> item = new LambdaQueryWrapper<>();
        item.eq(DicBusinessItemDo::getDicBusinessId, DicBusinessConstant.trackCompany).or();
        item.eq(DicBusinessItemDo::getDicBusinessId, DicBusinessConstant.fbaOrderOutPutTruck);
        List<DicBusinessItemDo> shopDoList = dicBusinessItemDao.selectList(item);
        return shopDoList.stream().collect(Collectors.toMap(shopDo -> shopDo.getId(), shopDo -> shopDo.getDicItemValue()));
    }


    private Map<Long, String> getShopIdMap() {
        List<ShopDo> shopDoList = shopDao.selectList(null);
        return shopDoList.stream().collect(Collectors.toMap(shopDo -> shopDo.getId(), shopDo -> shopDo.getShopName()));
    }

    private Map<Long, String> getStorageMap() {
        List<StorageDo> shopDoList = storageDao.selectList(null);
        return shopDoList.stream().collect(Collectors.toMap(shopDo -> shopDo.getId(), shopDo -> shopDo.getStorageName()));
    }

    private Map<Long, String> getCompanyIdMap() {
        List<CompanyDo> companyDoList = companyDao.selectList(null);
        return companyDoList.stream().collect(Collectors.toMap(shopDo -> shopDo.getId(), shopDo -> shopDo.getCompanyCode()));
    }

    @Override
    public void exportExcel(Map<String, String> map, HttpServletResponse response) throws IOException {
        //出库单导出
        List<CostRecordPageRes> costRecordListRes = new ArrayList<>(); //出库单导出
        //出库单导出
        List<OrderOutputNewVo> orderOutExcelExportVos = new ArrayList<>();
        //入库费用
        List<OrderInputExcelExportVo> orderInputExcelExportVos = new ArrayList<>();
        //仓储费
        List<StockFeeVo> stockFeeExcelExportVos = new ArrayList<>();
        //工单费用
        List<OrderWorkExcelExportVo> orderWorkExcelExportVos = new ArrayList<>();
        //退货费用
        List<OrderBackVo> orderBackExcelExportVos = new ArrayList<>();
        //头程费用
        List<HeaderBillExportVo> headerBillExportVos = new ArrayList<>();
        List<RepairBillExportVo> repairBillExportVos = new ArrayList<>();
        Map<Integer, String> typeMap = getTypeMap();
        Map<Long, String> shopIdMap = getShopIdMap();
        Map<Long, String> storageMap = getStorageMap();
        Map<Long, String> companyIdMap = getCompanyIdMap();
        Map<Long, String> tracIdMap = getTracIdMap();
        QueryWrapper<CostRecordDo> wrapper = queryBuild(map);
        List<CostRecordDo> costRecordDoList = dao.selectList(wrapper);
        if (!CollectionUtils.isEmpty(costRecordDoList)) {
            costRecordListRes = costRecordDoList.stream().map(new Function<CostRecordDo, CostRecordPageRes>() {
                @Override
                public CostRecordPageRes apply(CostRecordDo costRecordDo) {
                    CostRecordPageRes costRecordRes = BeanConvert.INSTANCE.costRecord2PageRes(costRecordDo);
                    costRecordRes.setType(typeMap.get(costRecordDo.getType()));
                    costRecordRes.setStatus(costRecordDo.getStatus() == null || 0 == costRecordDo.getStatus() ? "未入账" : "已扣款");
                    costRecordRes.setShop(shopIdMap.get(costRecordDo.getShopId()));
                    costRecordRes.setCompanyCode(companyIdMap.get(Long.parseLong(costRecordDo.getCompanyId())));
                    return costRecordRes;
                }
            }).collect(Collectors.toList());
            Map<Integer, List<CostRecordDo>> collect = costRecordDoList.stream().collect(Collectors.groupingBy(CostRecordDo::getType));
            for (Integer type : collect.keySet()) {
                List<CostRecordDo> costRecordDos = collect.get(type);
                List<String> idList = costRecordDos.stream().filter(costRecordDo -> costRecordDo.getType() == type)
                        .map(CostRecordDo::getThirdId).collect(Collectors.toList());
                if (type == 0) {//出库订单
                    List<String> pjIdList = costRecordDos.stream().filter(costRecordDo -> costRecordDo.getContent().startsWith("PJ"))
                            .map(CostRecordDo::getThirdId).collect(Collectors.toList());
                    List<String> orderIdList = costRecordDos.stream().filter(costRecordDo -> costRecordDo.getContent().startsWith("OT"))
                            .map(CostRecordDo::getThirdId).collect(Collectors.toList());
                    List<OrderOutputNewVo> excelExportVoList = handlerOrderOutPut(orderIdList, pjIdList, null, shopIdMap, storageMap, companyIdMap, tracIdMap);
                    if (excelExportVoList != null) {
                        orderOutExcelExportVos.addAll(excelExportVoList);
                    }
                } else if (type == 1) {//入库订单
                    for (CostRecordDo costRecordDo : costRecordDos) {
                        OrderInputExcelExportVo excelExportVo = handlerOrderInput(costRecordDo, storageMap, companyIdMap);
                        if (excelExportVo != null) {
                            orderInputExcelExportVos.add(excelExportVo);
                        }
                    }

                } else if (type == 2) {//仓储费
//                    for (CostRecordDo costRecordDo : costRecordDos) {
                    List<StockFeeVo> excelExportVo = handlerStockFee(idList, shopIdMap, storageMap, companyIdMap);
                    if (excelExportVo != null) {
                        stockFeeExcelExportVos.addAll(excelExportVo);
                    }
//                    }
                } else if (type == 3) {//定制费
                    for (CostRecordDo costRecordDo : costRecordDos) {
                        OrderWorkExcelExportVo excelExportVo = handlerWorkOrder(costRecordDo);
                        if (excelExportVo != null) {
                            orderWorkExcelExportVos.add(excelExportVo);
                        }
                    }
                } else if (type == 4) {//退货费
//                    for (CostRecordDo costRecordDo : costRecordDos) {
                    List<OrderBackVo> excelExportVo = handlerBackOrder(idList, shopIdMap, storageMap, companyIdMap);
                    if (excelExportVo != null) {
                        orderBackExcelExportVos.addAll(excelExportVo);
                    }
//                    }
                } else if (type == 5) {//FBA转运
//                    for (CostRecordDo costRecordDo : costRecordDos) {
//                        OrderOutExcelExportVo excelExportVo = handlerFbaOrderOutPut(costRecordDo);
//                        if (excelExportVo != null) {
//                            orderOutExcelExportVos.add(excelExportVo);
//                        }
//                    }
                    List<OrderOutputNewVo> excelExportVoList = handlerOrderOutPut(null, null, idList, shopIdMap, storageMap, companyIdMap, tracIdMap);
                    if (excelExportVoList != null) {
                        orderOutExcelExportVos.addAll(excelExportVoList);
                    }
                } else if (type == 6) { //头程费用
                    for (CostRecordDo costRecordDo : costRecordDos) {
                        HeaderBillExportVo exportVo = this.handlerHeaderFee(Long.parseLong(costRecordDo.getThirdId()));
                        if (exportVo != null) {
                            exportVo.setBillName(costRecordDo.getContent());
                            exportVo.setStatus(costRecordDo.getStatus());
                            headerBillExportVos.add(exportVo);
                        }
                    }
                } else if (type == 7) { //头程费用
                    List<String> collect1 = costRecordDos.stream().map(CostRecordDo::getThirdId).collect(Collectors.toList());
                    if (collect1 == null || collect1.size() < 1) continue;
                    List<RepairBillExportVo> exportVoList = handlerRepairFee(collect1);
                    repairBillExportVos.addAll(exportVoList);
                }
            }
        }

        // 创建参数对象（用来设定excel得sheet得内容等信息）
        ExportParams orderCommonParams = new ExportParams();
        // 设置sheet得名称
        orderCommonParams.setSheetName("总计");
        // 创建sheet1使用得map
        Map<String, Object> orderCommonMap = new HashMap<>();
        // title的参数为ExportParams类型，目前仅仅在ExportParams中设置了sheetName
        orderCommonMap.put("title", orderCommonParams);
        // 模版导出对应得实体类型
        orderCommonMap.put("entity", CostRecordPageRes.class);
        // sheet中要填充得数据
        orderCommonMap.put("data", costRecordListRes);

        // 创建参数对象（用来设定excel得sheet得内容等信息）
        ExportParams orderOutputParams = new ExportParams();
        // 设置sheet得名称
        orderOutputParams.setSheetName("出库费用");
        // 创建sheet1使用得map
        Map<String, Object> orderOutputMap = new HashMap<>();
        // title的参数为ExportParams类型，目前仅仅在ExportParams中设置了sheetName
        orderOutputMap.put("title", orderOutputParams);
        // 模版导出对应得实体类型
        orderOutputMap.put("entity", OrderOutputNewVo.class);
        // sheet中要填充得数据
        orderOutputMap.put("data", orderOutExcelExportVos);


        ExportParams orderInputParams = new ExportParams();
        orderInputParams.setSheetName("入库费用");
        // 创建sheet2使用得map
        Map<String, Object> orderInputMap = new HashMap<>();
        orderInputMap.put("title", orderInputParams);
        orderInputMap.put("entity", OrderInputExcelExportVo.class);
        orderInputMap.put("data", orderInputExcelExportVos);


        ExportParams stockFeeParams = new ExportParams();
        stockFeeParams.setSheetName("仓储费用");
        // 创建sheet3使用得map
        Map<String, Object> stockFeeMap = new HashMap<>();
        stockFeeMap.put("title", stockFeeParams);
        stockFeeMap.put("entity", StockFeeVo.class);
        stockFeeMap.put("data", stockFeeExcelExportVos);


        ExportParams orderWorkParams = new ExportParams();
        orderWorkParams.setSheetName("工单费用");
        // 创建sheet4使用得map
        Map<String, Object> orderWorkMap = new HashMap<>();
        orderWorkMap.put("title", orderWorkParams);
        orderWorkMap.put("entity", OrderWorkExcelExportVo.class);
        orderWorkMap.put("data", orderWorkExcelExportVos);


        ExportParams orderBackParams = new ExportParams();
        orderBackParams.setSheetName("退货费用");
        // 创建sheet4使用得map
        Map<String, Object> orderBackMap = new HashMap<>();
        orderBackMap.put("title", orderBackParams);
        orderBackMap.put("entity", OrderBackVo.class);
        orderBackMap.put("data", orderBackExcelExportVos);

        ExportParams headerParams = new ExportParams();
        headerParams.setSheetName("头程费用");
        // 创建sheet4使用得map
        Map<String, Object> headerMap = new HashMap<>();
        headerMap.put("title", headerParams);
        headerMap.put("entity", HeaderBillExportVo.class);
        headerMap.put("data", headerBillExportVos);

        ExportParams repairParams = new ExportParams();
        repairParams.setSheetName("维修费用");
        // 创建sheet4使用得map
        Map<String, Object> repairMap = new HashMap<>();
        repairMap.put("title", repairParams);
        repairMap.put("entity", RepairBillExportVo.class);
        repairMap.put("data", repairBillExportVos);


        // 将sheet1、sheet2、sheet3、sheet4使用得map进行包装
        List<Map<String, Object>> sheetsList = new ArrayList<>();
        sheetsList.add(orderCommonMap);
        sheetsList.add(orderOutputMap);
        sheetsList.add(orderInputMap);
        sheetsList.add(stockFeeMap);
        sheetsList.add(orderWorkMap);
        sheetsList.add(orderBackMap);
        sheetsList.add(headerMap);
        sheetsList.add(repairMap);
        ExcelUtils.exportExcel(sheetsList, "费用明细", response);
    }

    private List<RepairBillExportVo> handlerRepairFee(List<String> ids) {
        List<RepairBillExportVo> orderRepairDos = repairService.handlerBillListAfter(ids);
        return orderRepairDos;
    }

    public HeaderBillExportVo handlerHeaderFee(Long billId) {
        ManageBillDo billDo = manageBillDao.selectById(billId);
        if (billDo == null) {
            return null;
        }
        List<HeaderContainerDo> containerDoList = manageBillDao.getContainerNumber(billId);

        HeaderBillExportVo exportVo = new HeaderBillExportVo();
        exportVo.setBillLadingNumber(StringUtils.join(containerDoList.stream().map(HeaderContainerDo::getBillLadingNumber).collect(Collectors.toList()), ","));
        exportVo.setContainerNumber(StringUtils.join(containerDoList.stream().map(HeaderContainerDo::getContainerNumber).collect(Collectors.toList()), ","));
        exportVo.setFilePath(fileService.getFilePath(billDo.getFileId()));
        exportVo.setBillNo(billDo.getBillNo());
        List<ManageHeaderPriceItemDo> itemDoList = manageHeaderPriceItemDao.selectList(new LambdaQueryWrapper<ManageHeaderPriceItemDo>()
                .eq(ManageHeaderPriceItemDo::getBillId, billId).eq(ManageHeaderPriceItemDo::getType, "beginBill"));

        List<HeaderBillPriceItemExportVo> itemExportVoList = new ArrayList<>();
        for (ManageHeaderPriceItemDo itemDo : itemDoList) {
            HeaderBillPriceItemExportVo itemExportVo = new HeaderBillPriceItemExportVo();
            if (itemDo.getProjectId() != null) {
                StorageServiceProjectDo projectDo = storageServiceProjectDao.selectById(itemDo.getProjectId());
                if (projectDo == null) {
                    continue;
                }
                itemExportVo.setProjectName(projectDo.getProjectName());
            } else {
                itemExportVo.setProjectName(itemDo.getValue());
            }
            itemExportVo.setCurrencyType(itemDo.getCurrencyType());
            itemExportVo.setPrice(itemDo.getPrice());
            itemExportVo.setTax(itemDo.getTax());
            itemExportVoList.add(itemExportVo);
        }
        exportVo.setList(itemExportVoList);
        return exportVo;
    }

    private List<OrderBackVo> handlerBackOrder(List<String> idList, Map<Long, String> shopIdMap, Map<Long, String> storageMap, Map<Long, String> companyMap) {
        Map<Long, String> resultMap = dicBusinessItemDao.selectList(new LambdaQueryWrapper<DicBusinessItemDo>()
                .eq(DicBusinessItemDo::getDicBusinessId, DicBusinessConstant.orderBackResult)).stream().collect(Collectors.toMap(DicBusinessItemDo::getId, DicBusinessItemDo::getDicItemValue));
        Map<Long, String> statusMap = dicBusinessItemDao.selectList(new LambdaQueryWrapper<DicBusinessItemDo>()
                .eq(DicBusinessItemDo::getDicBusinessId, DicBusinessConstant.orderBackStatus)).stream().collect(Collectors.toMap(DicBusinessItemDo::getId, DicBusinessItemDo::getDicItemValue));

        List<OrderBackDto> orderBackDtos = orderBackDao.selectDataAndProductList(idList);
        if (orderBackDtos == null) return new ArrayList<>();
        AtomicReference<String> lastOrderNumber = new AtomicReference<>();
        List<OrderBackVo> collect = orderBackDtos.stream().map(orderBackDto -> {

            OrderBackVo orderBackVo = BeanConvert.INSTANCE.orderBack2Export(orderBackDto);
            if (lastOrderNumber.get() != null && lastOrderNumber.get().equals(orderBackDto.getOrderOutNumber())) {
                orderBackVo.setOrderOutNumber(null);
            }
            lastOrderNumber.set(orderBackVo.getOrderOutNumber());
            orderBackVo.setShopName(shopIdMap.get(orderBackDto.getShopId()));
            orderBackVo.setStorageName(storageMap.get(orderBackDto.getStorageId()));
            orderBackVo.setCompanyCode(companyMap.get(orderBackDto.getCompanyId()));
            orderBackVo.setResult(resultMap.get(orderBackDto.getBackResult()));
            orderBackVo.setStatus(statusMap.get(orderBackDto.getStatus()));
            if (orderBackDto.getProductDtoList() != null && orderBackDto.getProductDtoList().size() > 0) {
                List<OrderBackProductVo> collectProdcut = new ArrayList<>();
                for (OrderBackProductDto orderBackProductDto : orderBackDto.getProductDtoList()) {
                    OrderBackProductVo productVo = BeanConvert.INSTANCE.orderBackProduct2Export(orderBackProductDto);
                    if (StringUtils.isBlank(productVo.getSku())) {
                        productVo.setSku("未知商品");
                    }
                    collectProdcut.add(productVo);
                }
                orderBackVo.setProductVoList(collectProdcut);
            }
            return orderBackVo;
        }).filter(orderBackDto -> orderBackDto.getOrderOutNumber() != null).collect(Collectors.toList());
        return collect;
    }

    private OrderBackExcelExportVo handlerBackOrder(CostRecordDo costRecordDo) {
        OrderBackExcelExportVo excelExportVo = new OrderBackExcelExportVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        OrderBackDo orderBackDo = orderBackDao.selectById(costRecordDo.getThirdId());
        if (orderBackDo == null) return null;
        OrderBackVo orderOutputVo = new OrderBackVo();
        ShopDo shopDo = shopDao.selectById(orderBackDo.getShopId());
        if (shopDo != null) {
            orderOutputVo.setShopName(shopDo.getShopName());
        }
        StorageDo storageDo = storageDao.selectById(orderBackDo.getStorageId());
        if (storageDo != null) {
            orderOutputVo.setStorageName(storageDo.getStorageName());
        }
        orderOutputVo.setShipFee(orderBackDo.getShipFee());
        orderOutputVo.setOperateFee(orderBackDo.getOperateFee());
        orderOutputVo.setTaxFee(orderBackDo.getTaxFee());
        orderOutputVo.setOrderOutNumber(orderBackDo.getOrderOutNumber());
        DicBusinessItemDo businessItemDo = dicBusinessItemDao.selectById(orderBackDo.getBackResult());
        if (businessItemDo != null) {
            orderOutputVo.setResult(businessItemDo.getDicItemValue());
        }
        DicBusinessItemDo businessItemDo1 = dicBusinessItemDao.selectById(orderBackDo.getStatus());
        if (businessItemDo1 != null) {
            orderOutputVo.setStatus(businessItemDo1.getDicItemValue());
        }
        orderOutputVo.setTrackNumber(orderBackDo.getTrackNumber());
        orderOutputVo.setTrackName(orderBackDo.getTrackName());
        LambdaQueryWrapper<OrderBackProductDo> productDoLambdaQueryWrappe = new LambdaQueryWrapper<>();
        productDoLambdaQueryWrappe.eq(OrderBackProductDo::getOrderId, orderBackDo.getId());
        List<OrderBackProductDo> productDoList = orderBackProductDao.selectList(productDoLambdaQueryWrappe);
        StringBuilder sb = new StringBuilder();
        int count = 0;
        if (productDoList != null && productDoList.size() > 0) {
            List<OrderBackProductVo> collectProdcut = new ArrayList<>();
            for (OrderBackProductDo orderBackProductDo : productDoList) {
                ProductInfoDo productInfoDo = productInfoDao.selectById(orderBackProductDo.getProductId());
                if (productInfoDo == null) continue;
                OrderBackProductVo productVo = new OrderBackProductVo();
                productVo.setSku(productVo.getSku());
                productVo.setSkuCount(productVo.getSkuCount());
                collectProdcut.add(productVo);
            }
            orderOutputVo.setProductVoList(collectProdcut);
        }
        excelExportVo.setOrderBackVoList(orderOutputVo);
        return excelExportVo;
    }

    private OrderOutExcelExportVo handlerFbaOrderOutPut(CostRecordDo costRecordDo) {
        OrderOutExcelExportVo excelExportVo = new OrderOutExcelExportVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        FbaOrderOutputDo orderOutputDo = fbaOrderOutputDao.selectById(costRecordDo.getThirdId());
        if (orderOutputDo == null) return null;
        //出库单
        List<OrderOutputVo> orderOutputVoList = new ArrayList<>();
        OrderOutputVo orderOutputVo = new OrderOutputVo();
        ShopDo shopDo = shopDao.selectById(orderOutputDo.getShopId());
        if (shopDo != null) {
            orderOutputVo.setShopName(shopDo.getShopName());
        }
        StorageDo storageDo = storageDao.selectById(orderOutputDo.getStorageId());
        if (storageDo != null) {
            orderOutputVo.setStorageName(storageDo.getStorageName());
        }
        orderOutputVo.setShipFee(orderOutputDo.getShipFee());
        orderOutputVo.setOperateFee(orderOutputDo.getOperateFee());
        orderOutputVo.setTaxFee(orderOutputDo.getTaxFee());
        orderOutputVo.setOutFee(orderOutputDo.getOutFee());
        orderOutputVo.setOrderOutputNumber(orderOutputDo.getFbaOrderOutputNumber());
        orderOutputVo.setOrderNo(orderOutputDo.getOrderNo());
        DicBusinessItemDo dicBusinessItemDo = dicBusinessItemDao.selectById(orderOutputDo.getTrackId());
        if (dicBusinessItemDo != null)
            orderOutputVo.setTrackName(dicBusinessItemDo.getDicItemValue());
        orderOutputVo.setTrackNumber(orderOutputDo.getTrackNumber());
        FbaStorageDo fbaStorageDo = fbaStorageDao.selectById(orderOutputDo.getFbaStorageId());
        if (fbaStorageDo != null) {
            orderOutputVo.setAddress(fbaStorageDo.getFbaName());
            orderOutputVo.setArea(fbaStorageDo.getFbaCode());
        } else {
            orderOutputVo.setAddress(orderOutputDo.getAddress());
        }
        orderOutputVo.setCreateTime(orderOutputDo.getCreateTime());

        List<OrderOutPackageVo> packageVoList = new ArrayList<>();
        LambdaQueryWrapper<FbaOrderOutputPackageDo> packageDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        packageDoLambdaQueryWrapper.eq(FbaOrderOutputPackageDo::getFbaOrderOutputId, orderOutputDo.getId());
        List<FbaOrderOutputPackageDo> packageDoList = fbaOrderOutputPackageDao.selectList(packageDoLambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(packageDoList)) {
            for (FbaOrderOutputPackageDo packageDo : packageDoList) {
                OrderOutPackageVo orderOutPackageVo = new OrderOutPackageVo();
                try {
                    orderOutPackageVo.setHeight((float) packageDo.getHeight());
                    orderOutPackageVo.setWidth((float) packageDo.getWidth());
                    orderOutPackageVo.setLength((float) packageDo.getLength());
                    orderOutPackageVo.setPackageCount(packageDo.getCount());
                } catch (Exception e) {
//                    orderOutPackageVo.setVolume(0D);
                }
                orderOutPackageVo.setWeight((float) packageDo.getWeight());
                List<OrderOutProductVo> productVoList = new ArrayList<>();
                LambdaQueryWrapper<FbaOrderOutputProductDo> productDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                productDoLambdaQueryWrapper.eq(FbaOrderOutputProductDo::getPackageId, packageDo.getId());
                List<FbaOrderOutputProductDo> orderOutputProductDoList = fbaOrderOutputProductDao.selectList(productDoLambdaQueryWrapper);
                if (!CollectionUtils.isEmpty(orderOutputProductDoList)) {
                    for (FbaOrderOutputProductDo orderOutputProductDo : orderOutputProductDoList) {
                        OrderOutProductVo orderOutProductVo = new OrderOutProductVo();
                        orderOutProductVo.setCount(orderOutputProductDo.getCount());
                        ProductInfoDo productInfoDo = productInfoDao.selectById(orderOutputProductDo.getProductId());
                        if (productInfoDo != null) {
                            orderOutProductVo.setCommoditySku(productInfoDo.getCommoditySku());
                        } else {
                            orderOutProductVo.setCommoditySku("SKU不存在或被删除");
                        }
                        productVoList.add(orderOutProductVo);
                    }
                }

                orderOutPackageVo.setProductVoList(productVoList);
                packageVoList.add(orderOutPackageVo);
            }
        }
        orderOutputVo.setPackageVoList(packageVoList);
        orderOutputVoList.add(orderOutputVo);
        excelExportVo.setOrderOutputVoList(orderOutputVoList);

        return excelExportVo;
    }

    private OrderWorkExcelExportVo handlerWorkOrder(CostRecordDo costRecordDo) {
        OrderWorkExcelExportVo excelExportVo = new OrderWorkExcelExportVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        OrderWorkDo orderWorkDo = orderWorkDao.selectById(costRecordDo.getThirdId());
        if (orderWorkDo == null) return null;
        List<OrderWorkVo> orderWorkVoList = new ArrayList<>();
        OrderWorkVo orderWorkVo = new OrderWorkVo();
        orderWorkVo.setWorkNumber(orderWorkDo.getWorkNumber());
        ShopDo shopDo = shopDao.selectById(orderWorkDo.getShopId());
        if (shopDo != null) {
            orderWorkVo.setShopName(shopDo.getShopName());
        }
        StorageDo storageDo = storageDao.selectById(orderWorkDo.getStorageId());
        if (storageDo != null) {
            orderWorkVo.setStorageName(storageDo.getStorageName());
        }
        DicBusinessItemDo businessItemDo = dicBusinessItemDao.selectById(orderWorkDo.getWorkType());
        if (businessItemDo != null) {
            orderWorkVo.setWorkType(businessItemDo.getDicItemValue());
        }

        LambdaQueryWrapper<OrderWorkPriceDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderWorkPriceDo::getOrderWorkId, orderWorkDo.getId());
        List<OrderWorkPriceDo> workPriceDoList = orderWorkPriceDao.selectList(wrapper);
        if (!CollectionUtils.isEmpty(workPriceDoList)) {
            List<OrderWorkPriceVo> orderWorkPriceVoList = new ArrayList<>();
            for (OrderWorkPriceDo orderWorkPriceDo : workPriceDoList) {
                OrderWorkPriceVo orderWorkPriceVo = new OrderWorkPriceVo();
                orderWorkPriceVo.setName(orderWorkPriceDo.getName());
                orderWorkPriceVo.setPrice(orderWorkPriceDo.getPrice());
                orderWorkPriceVo.setType(orderWorkPriceDo.getCurrency().intValue());
                orderWorkPriceVoList.add(orderWorkPriceVo);
            }
            orderWorkVo.setOrderWorkPriceVoList(orderWorkPriceVoList);
        }

        orderWorkVoList.add(orderWorkVo);
        excelExportVo.setOrderWorkVoList(orderWorkVoList);
        return excelExportVo;
    }

    private List<StockFeeVo> handlerStockFee(List<String> ids, Map<Long, String> shopMap, Map<Long, String> storageMap, Map<Long, String> companyIdMap) {
        if (ids == null || ids.size() < 1) return new ArrayList<>();
        List<StockHistoryDo> stockHistoryDoList = stockHistoryDao.selectBatchIds(ids);
        return stockHistoryDoList.stream().map(stockHistoryDo -> {
            StockFeeVo stockFeeVo = new StockFeeVo();
            stockFeeVo.setCompanyCode(companyIdMap.get(stockHistoryDo.getCompanyId()));
            stockFeeVo.setShopName(shopMap.get(stockHistoryDo.getShopId()));
            stockFeeVo.setStorageName(storageMap.get(stockHistoryDo.getStorageId()));
            stockFeeVo.setCount(stockHistoryDo.getCanSell() + stockHistoryDo.getCanotSell());
            stockFeeVo.setCostAmount(new BigDecimal(stockHistoryDo.getCost()).setScale(0, RoundingMode.HALF_UP).doubleValue());
            stockFeeVo.setCreateDate(stockHistoryDo.getCreateDate());
            stockFeeVo.setTotalVolume(stockHistoryDo.getTotalVolume());
            stockFeeVo.setV1(stockHistoryDo.getV1Volume());
            stockFeeVo.setV2(stockHistoryDo.getV2Volume());
            stockFeeVo.setV3(stockHistoryDo.getV3Volume());
            double v = stockHistoryDo.getTotalVolume() - stockHistoryDo.getV1Volume() -
                    stockHistoryDo.getV2Volume() - stockHistoryDo.getV3Volume();
            if (v > 0)
                stockFeeVo.setV4(new BigDecimal(v).setScale(2, RoundingMode.HALF_UP).doubleValue());
            else
                stockFeeVo.setV4(0D);
            return stockFeeVo;
        }).collect(Collectors.toList());
    }

    private StockFeeExcelExportVo handlerStockFee(CostRecordDo costRecordDo) {
        StockFeeExcelExportVo excelExportVo = new StockFeeExcelExportVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        ShopDo shopDo = shopDao.selectById(costRecordDo.getShopId());
        if (shopDo != null) {
            excelExportVo.setShopName(shopDo.getShopName());
        }
        StockHistoryDo stockHistoryDo = stockHistoryDao.selectById(costRecordDo.getThirdId());
        List<StockFeeVo> stockFeeVoList = new ArrayList<>();
        if (stockHistoryDo != null) {
            StockFeeVo stockFeeVo = new StockFeeVo();

            StorageDo storageDo = storageDao.selectById(stockHistoryDo.getStorageId());
            if (storageDo != null) {
                stockFeeVo.setStorageName(storageDo.getStorageName());
            }
            stockFeeVo.setCount(stockHistoryDo.getCanotSell() + stockHistoryDo.getCanotSell());
            stockFeeVo.setCreateDate(stockHistoryDo.getCreateDate());
            stockFeeVo.setTotalVolume(stockHistoryDo.getTotalVolume());
            stockFeeVo.setV1(stockHistoryDo.getV1Volume());
            stockFeeVo.setV2(stockHistoryDo.getV2Volume());
            stockFeeVo.setV3(stockHistoryDo.getV3Volume());
            stockFeeVo.setV4(new BigDecimal(stockHistoryDo.getTotalVolume() - stockHistoryDo.getV1Volume() -
                    stockHistoryDo.getV2Volume() - stockHistoryDo.getV3Volume()).setScale(2, RoundingMode.HALF_UP).doubleValue());
            stockFeeVoList.add(stockFeeVo);
        }
        excelExportVo.setStockFeeVoList(stockFeeVoList);

        return excelExportVo;
    }

    private OrderInputExcelExportVo handlerOrderInput(CostRecordDo costRecordDo, Map<Long, String> storageMap, Map<Long, String> companyIdMap) {

        OrderInputExcelExportVo excelExportVo = new OrderInputExcelExportVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        OrderInputDo orderInputDo = orderInputDao.selectById(costRecordDo.getThirdId());
        if (orderInputDo == null) return null;
        OrderInputVo orderInputVo = new OrderInputVo();
        orderInputVo.setCarriageTrackNo(orderInputDo.getCarriageTrackNo());
        orderInputVo.setStorageName(storageMap.get(orderInputDo.getStorageId()));
        orderInputVo.setCompanyCode(companyIdMap.get(orderInputDo.getCompanyId()));
        orderInputVo.setClearanceFee(orderInputDo.getClearanceFee());
        orderInputVo.setInputFee(0F);//todo 去费用表里查
        orderInputVo.setOrderInputNumber(orderInputDo.getOrderInputNumber());
        orderInputVo.setSeaFee(orderInputDo.getSeaFee());
        orderInputVo.setSendType(orderInputDo.getSendType());
        orderInputVo.setServiceFee(orderInputDo.getServiceFee());
        orderInputVo.setTaxFee(orderInputDo.getTaxFee());
        orderInputVo.setVolume(orderInputDo.getVolume());
        List<OrderInputVo> orderInputVos = new ArrayList<>();
        orderInputVos.add(orderInputVo);
        excelExportVo.setOrderInputVoList(orderInputVos);

        return excelExportVo;
    }


    private List<OrderOutputNewVo> handlerOrderOutPut(List<String> outIds, List<String> pjIds, List<String> fbaOutIds, Map<Long, String> shopIdMap, Map<Long, String> storageMap, Map<Long, String> companyIdMap, Map<Long, String> tracIdMap) {
        List<OrderCostExportDto> orderCostExportDtos = new ArrayList<>();
        if (outIds != null && outIds.size() > 0) {
            orderCostExportDtos = orderOutputDao.selectOrderCostExcelExport(outIds);
        }
        if (fbaOutIds != null && fbaOutIds.size() > 0) {
            List<OrderCostExportDto> fbaOrderCostExportDtos = fbaOrderOutputDao.selectOrderCostExcelExport(fbaOutIds);
            if (fbaOrderCostExportDtos != null)
                orderCostExportDtos.addAll(fbaOrderCostExportDtos);
        }
        if (pjIds != null && pjIds.size() > 0) {
            List<OrderCostExportDto> pjCostExportDtos = replacementDao.selectOrderCostExcelExport(pjIds);
            if (pjCostExportDtos != null)
                orderCostExportDtos.addAll(pjCostExportDtos);
        }
        String lastOrderNumber = "";
        Long lastPackageId = 0L;
        List<OrderOutputNewVo> collect = new ArrayList<>();
        List<OrderOutPackageVo> packageCollect = new ArrayList<>();
        List<OrderOutProductVo> productCollect = new ArrayList<>();
        for (OrderCostExportDto orderCostExportDto : orderCostExportDtos) {
            if (!lastOrderNumber.equals(orderCostExportDto.getOrderOutputNumber())) {
                lastOrderNumber = orderCostExportDto.getOrderOutputNumber();
                OrderOutputNewVo orderOutputVo = BeanConvert.INSTANCE.orderCostDto2ExportVo(orderCostExportDto);
                orderOutputVo.setShopName(shopIdMap.get(orderCostExportDto.getShopId()));
                orderOutputVo.setCompanyCode(companyIdMap.get(orderCostExportDto.getCompanyId()));
                orderOutputVo.setStorageName(storageMap.get(orderCostExportDto.getStorageId()));
                String trackName = "佐川即便";
                if (orderCostExportDto.getTrackId() != null) {
                    trackName = tracIdMap.get(orderCostExportDto.getTrackId());
                }
                orderOutputVo.setTrackName(trackName);
                collect.add(orderOutputVo);

            }
            if (orderCostExportDto.getPackageId() == null) continue;
            if (!lastPackageId.equals(orderCostExportDto.getPackageId())) {
                lastPackageId = orderCostExportDto.getPackageId();
                OrderOutputNewVo orderOutputVo = collect.get(collect.size() - 1);
                if (orderOutputVo.getPackageVoList() == null) {
                    orderOutputVo.setPackageVoList(new ArrayList<>());
                }
                OrderOutPackageVo orderOutPackageVo = BeanConvert.INSTANCE.orderCostDto2PackageExportVo(orderCostExportDto);
                orderOutputVo.getPackageVoList().add(orderOutPackageVo);
            }
            OrderOutputNewVo orderOutputVo = collect.get(collect.size() - 1);
            if (orderOutputVo.getPackageVoList() == null) {
                orderOutputVo.setPackageVoList(new ArrayList<>());
            }
            OrderOutPackageVo orderOutPackageVo = orderOutputVo.getPackageVoList().get(orderOutputVo.getPackageVoList().size() - 1);
            if (orderOutPackageVo.getProductVoList() == null) {
                orderOutPackageVo.setProductVoList(new ArrayList<>());
            }
            OrderOutProductVo orderOutProductVo = BeanConvert.INSTANCE.orderCostDto2ProductExportVo(orderCostExportDto);
            orderOutPackageVo.getProductVoList().add(orderOutProductVo);

        }
//        List<OrderOutputNewVo> collect = orderCostExportDtos.stream().map(orderCostExportDto -> {
//            if (!lastOrderNumber.equals(orderCostExportDto.getOrderOutputNumber())){
//
//            }
//            OrderOutputNewVo orderOutputVo = BeanConvert.INSTANCE.orderCostDto2ExportVo(orderCostExportDto);
//            orderOutputVo.setShopName(shopIdMap.get(orderCostExportDto.getShopId()));
//            orderOutputVo.setCompanyCode(companyIdMap.get(orderCostExportDto.getCompanyId()));
//            orderOutputVo.setStorageName(storageMap.get(orderCostExportDto.getStorageId()));
//            String trackName = "佐川即便";
//            if (orderCostExportDto.getTrackId() != null) {
//                trackName = tracIdMap.get(orderCostExportDto.getTrackId());
//            }
//            orderOutputVo.setTrackName(trackName);
//            return orderOutputVo;
//        }).collect(Collectors.toList());
        return collect;
    }

    private OrderOutExcelExportVo handlerOrderOutPut(CostRecordDo costRecordDo, CostRecordPageRes costRecordRes) {
        //出库单
        List<OrderOutputVo> orderOutputVoList = new ArrayList<>();
        OrderOutExcelExportVo excelExportVo = new OrderOutExcelExportVo();
        OrderOutputVo orderOutputVo = new OrderOutputVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        if (costRecordDo.getContent().contains("PJ")) {
            ReplacementDo orderOutputDo = replacementDao.selectById(costRecordDo.getThirdId());
            if (orderOutputDo == null) return null;
            ShopDo shopDo = shopDao.selectById(orderOutputDo.getOutputShopId());
            if (shopDo != null) {
                orderOutputVo.setShopName(shopDo.getShopName());
            }
            StorageDo storageDo = storageDao.selectById(orderOutputDo.getOutputStorageId());
            if (storageDo != null) {
                orderOutputVo.setStorageName(storageDo.getStorageName());
            }
            orderOutputVo.setShipFee(costRecordDo.getCostAmount());
            orderOutputVo.setOrderOutputNumber(orderOutputDo.getOrderNo());
            orderOutputVo.setOrderNo(orderOutputDo.getOutputOrderNo());
            orderOutputVo.setTrackNumber(orderOutputDo.getTrackNumber());
            orderOutputVo.setAddress(orderOutputDo.getOutputAddress());
            orderOutputVo.setCreateTime(orderOutputDo.getUpdateTime());
        } else {
            OrderOutputDo orderOutputDo = orderOutputDao.selectById(costRecordDo.getThirdId());
            if (orderOutputDo == null) return null;
            ShopDo shopDo = shopDao.selectById(orderOutputDo.getShopId());
            if (shopDo != null) {
                orderOutputVo.setShopName(shopDo.getShopName());
            }
            StorageDo storageDo = storageDao.selectById(orderOutputDo.getStorageId());
            if (storageDo != null) {
                orderOutputVo.setStorageName(storageDo.getStorageName());
            }
            orderOutputVo.setShipFee(orderOutputDo.getShipFee());
            orderOutputVo.setOperateFee(orderOutputDo.getOperateFee());
            orderOutputVo.setTaxFee(orderOutputDo.getTaxFee());
            orderOutputVo.setOutFee(orderOutputDo.getOutFee());
            orderOutputVo.setOrderOutputNumber(orderOutputDo.getOrderOutputNumber());
            orderOutputVo.setOrderNo(orderOutputDo.getOrderNo());
            orderOutputVo.setTrackNumber(orderOutputDo.getTrackNumber());
            DicBusinessItemDo dicBusinessItemDo = dicBusinessItemDao.selectById(orderOutputDo.getTrackId());
            if (dicBusinessItemDo != null)
                orderOutputVo.setTrackName(dicBusinessItemDo.getDicItemValue());
            orderOutputVo.setAddress(orderOutputDo.getAddress());
            orderOutputVo.setArea(StringsUtil.getJpAreaByEnglishCode(orderOutputDo.getArea()));
            orderOutputVo.setCreateTime(orderOutputDo.getUpdateTime());

            List<OrderOutPackageVo> packageVoList = new ArrayList<>();
            LambdaQueryWrapper<OrderOutputPackageDo> packageDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            packageDoLambdaQueryWrapper.eq(OrderOutputPackageDo::getOrderOutputId, orderOutputDo.getId());
            List<OrderOutputPackageDo> packageDoList = orderOutputPackageDao.selectList(packageDoLambdaQueryWrapper);
            if (!CollectionUtils.isEmpty(packageDoList)) {
                for (OrderOutputPackageDo packageDo : packageDoList) {
                    OrderOutPackageVo orderOutPackageVo = new OrderOutPackageVo();
                    try {
                        orderOutPackageVo.setHeight((float) packageDo.getHeight());
                        orderOutPackageVo.setWidth((float) packageDo.getWidth());
                        orderOutPackageVo.setLength((float) packageDo.getLength());
                        orderOutPackageVo.setPackageCount(packageDo.getCount());
                    } catch (Exception e) {
//                    orderOutPackageVo.setVolume(0D);
                    }
                    orderOutPackageVo.setWeight((float) packageDo.getWeight());
                    List<OrderOutProductVo> productVoList = new ArrayList<>();
                    LambdaQueryWrapper<OrderOutputProductDo> productDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    productDoLambdaQueryWrapper.eq(OrderOutputProductDo::getPackageId, packageDo.getId());
                    List<OrderOutputProductDo> orderOutputProductDoList = orderOutputProductDao.selectList(productDoLambdaQueryWrapper);
                    if (!CollectionUtils.isEmpty(orderOutputProductDoList)) {
                        for (OrderOutputProductDo orderOutputProductDo : orderOutputProductDoList) {
                            OrderOutProductVo orderOutProductVo = new OrderOutProductVo();
                            orderOutProductVo.setCount(orderOutputProductDo.getCount());
                            ProductInfoDo productInfoDo = productInfoDao.selectById(orderOutputProductDo.getProductId());
                            if (productInfoDo != null) {
                                orderOutProductVo.setCommoditySku(productInfoDo.getCommoditySku());
                            } else {
                                orderOutProductVo.setCommoditySku(orderOutputDo.getCommoditySku());
                            }
                            productVoList.add(orderOutProductVo);
                        }
                    }

                    orderOutPackageVo.setProductVoList(productVoList);
                    packageVoList.add(orderOutPackageVo);
                }
            }
            orderOutputVo.setPackageVoList(packageVoList);
        }
        orderOutputVoList.add(orderOutputVo);
        excelExportVo.setOrderOutputVoList(orderOutputVoList);

        return excelExportVo;
    }

    //查询分页列表之后对列表数据的处理
    private List<CostRecordListRes> handlerListAfter(List<CostRecordDo> list) {
        List<CostRecordListRes> result = new ArrayList<>();
        Map<Integer, String> typeMap = this.getTypeMap();
        for (CostRecordDo costRecordDo :
                list) {
            CostRecordListRes costRecordRes = new CostRecordListRes();
            BeanUtils.copyProperties(costRecordDo, costRecordRes);
            costRecordRes.setType(typeMap.get(costRecordDo.getType()));
            costRecordRes.setStatus(costRecordDo.getStatus() == null || 0 == costRecordDo.getStatus() ? "未入账" : "已扣款");
            costRecordRes.setAccountType(costRecordDo.getAccountType() == null || costRecordDo.getAccountType() == 1 ? "日币账户" : "人民币账户");
            ShopDo shopDo = shopDao.selectById(costRecordDo.getShopId());
            if (shopDo != null) {
                costRecordRes.setShop(shopDo.getShopName());
            }
            CompanyDo companyDo = companyDao.selectById(costRecordDo.getCompanyId());
            if (companyDo != null) {
                costRecordRes.setCompanyCode(companyDo.getCompanyCode());
            }

            result.add(costRecordRes);
        }
        return result;
    }

    private IPage<CostRecordDo> pageInit(Map<String, String> map) {
        IPage<CostRecordDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<CostRecordDo> queryBuild(Map<String, String> map) {
        QueryWrapper<CostRecordDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("companyId"))) {
            wrapper.eq("company_id", map.get("companyId"));
        }

        if (StringUtils.isNotBlank(map.get("type"))) {
            wrapper.eq("type", map.get("type"));
        }
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("content", map.get("keyword").replaceAll(" ", "%")
                    .replaceAll("　", "%"));
        }
        if (StringUtils.isNotBlank(map.get("dateType"))) {
            if ("payTime".equals(map.get("dateType"))) {
                if (StringUtils.isNotBlank(map.get("dateStart"))) {
                    wrapper.ge("pay_time", map.get("dateStart"));
                }
                if (StringUtils.isNotBlank(map.get("dateEnd"))) {
                    wrapper.lt("pay_time", TimeUtils.addDaysFormat(map.get("dateEnd"), "yyyy-MM-dd", 1));
                }
                wrapper.orderByDesc("pay_time", "id");

            } else {
                if (StringUtils.isNotBlank(map.get("dateStart"))) {
                    wrapper.ge("create_time", map.get("dateStart"));
                }
                if (StringUtils.isNotBlank(map.get("dateEnd"))) {
                    wrapper.lt("create_time", TimeUtils.addDaysFormat(map.get("dateEnd"), "yyyy-MM-dd", 1));
                }
                wrapper.orderByDesc("create_time", "id");
            }
        }
        return wrapper;
    }


}
